package com.neulogistics.contrib.warehouse.domain.model.aggregate;

import cn.hutool.json.JSONArray;
import com.neulogistics.contrib.warehouse.domain.exception.WarehouseDomainException;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.CirculationDetail;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.CirculationStatus;
import com.neulogistics.contrib.warehouse.domain.model.enumeration.CirculationType;
import com.neulogistics.contrib.warehouse.domain.model.valueobject.GoodsItem;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.ddd.domain.seedwork.Entity;
import com.neulogistics.infrastructure.ddd.domain.seedwork.IAggregateRoot;
import lombok.Data;

import java.util.Date;
import java.util.List;

@Data
public class CirculationOrder extends Entity<Long> implements IAggregateRoot {
    /**
     * 出入库单id
     */
    private Long circulationId;

    /**
     * 所属库房id
     */
    private Integer warehouseId;

    /**
     * 流动类型: 0:入库；1:出库
     */
    private CirculationType circulationType;

    /**
     * 流动细节: 0:进货入库；1:调拨入库；2:退货入库；3:调拨出库；4:退货出库；5:取货出库
     */
    private CirculationDetail circulationDetail;

    /**
     * 调拨单id
     */
    private Long transferId;

    /**
     * 任务id
     */
    private Long taskId;

    /**
     * 货物数组[{商品id、计量单位、数量}]
     */
    private List<GoodsItem> goodsItems;

    /**
     * 状态: 0:进行中；1:已完成；2:已取消
     */
    private CirculationStatus circulationStatus;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 完成时间
     */
    private Date completeTime;

    /**
     * 最后操作人
     */
    private Integer lastOperator;

    /**
     * 是否删除
     */
    private Integer deleted;

    public CirculationOrder() {
    }

    public CirculationOrder(Long circulationId, Integer warehouseId,
                            CirculationType circulationType, CirculationDetail circulationDetail,
                            Long transferId, List<GoodsItem> goodsItems) {
        this.circulationId = circulationId;
        this.warehouseId = warehouseId;
        this.circulationType = circulationType;
        this.circulationDetail = circulationDetail;
        this.transferId = transferId;
        this.goodsItems = goodsItems;
        this.circulationStatus = CirculationStatus.In_Progress;
    }


    public void checkCirculationTypeAndDetail(CirculationType circulationType) throws WarehouseDomainException {
        if(circulationType != this.circulationType){
            throw new WarehouseDomainException(ErrorCode.FORBIDDEN_ERROR, "出入库单状态异常！");
        }

        if(circulationType == CirculationType.Inbound){
            if (circulationDetail.id < 0
                    || circulationDetail.id > CirculationDetail.Return_Inbound_Id){
                throw new WarehouseDomainException(ErrorCode.FORBIDDEN_ERROR, "入库单状态异常！");
            }
        }

        if(circulationType == CirculationType.Outbound){
            if (circulationDetail.id < CirculationDetail.Transfer_Outbound_Id
                    || circulationDetail.id > CirculationDetail.Pick_Up_Outbound_Id){
                throw new WarehouseDomainException(ErrorCode.FORBIDDEN_ERROR, "出库单状态异常！");
            }
        }
    }

}
